package org.esk.dablog.web.forms;

import org.acegisecurity.annotation.Secured;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.esk.dablog.web.util.RequestUtils;
import org.esk.dablog.service.EntryManager;
import org.esk.dablog.service.UserManager;
import org.esk.dablog.model.entries.Ride;
import org.esk.dablog.model.Author;
import org.esk.dablog.model.Comment;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This class
 * User: jc
 * Date: 01.12.2006
 * Time: 19:19:24
 * $Id:$
 */
@Secured({"ROLE_REGISTERED_USER"})
public class JoinRideForm extends ParameterizableViewController {
    private EntryManager entryManager;
    private UserManager authorManager;

    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        long id = RequestUtils.extractLongParameter(request);
        Ride r = (Ride) entryManager.getEntry(Ride.class, id);
        Author a = authorManager.getAuthorByUsername(request.getRemoteUser());
        Comment c = r.join(a);
        c.setIp(request.getRemoteAddr());
        entryManager.saveComment(c);
        return new ModelAndView(new RedirectView(getViewName() + "/" + id));
    }



    public void setEntryManager(EntryManager entryManager) {
        this.entryManager = entryManager;
    }

    public void setAuthorManager(UserManager authorManager) {
        this.authorManager = authorManager;
    }
}
